package bi.example.demo9;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/DeleteClassServlet")
public class DeleteClassServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String classId = req.getParameter("classid");
        String url = "jdbc:mysql://localhost:3306/xscj_x?serverTimezone=UTC";
        String dbUser = "root";
        String dbPass = "123456";

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(url, dbUser, dbPass);
            String sql = "DELETE FROM class WHERE classid = ?";
            PreparedStatement prep = conn.prepareStatement(sql);
            prep.setString(1, classId);
            int rowsAffected = prep.executeUpdate();

            if (rowsAffected > 0) {
                resp.getWriter().write("success");
            } else {
                resp.getWriter().write("failed: no rows affected");
            }

            prep.close();
            conn.close();
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
